package com.mingqijia.gassafety.authority.vcc.oauth;

import com.mingqijia.gassafety.authority.vcc.valueobject.ManagerVO;
import com.mingqijia.gassafety.shared.utils.ApplicationUtils;
import org.springframework.security.access.ConfigAttribute;
import org.springframework.security.access.SecurityConfig;
import org.springframework.security.web.FilterInvocation;
import org.springframework.security.web.access.intercept.FilterInvocationSecurityMetadataSource;
import org.springframework.stereotype.Component;
import org.springframework.util.AntPathMatcher;

import java.util.Collection;

/**
 * 通过当前的请求地址，获取该地址需要的用户角色。
 * @author Damon S.
 * @version v0.2
 * @date 2022-02-27 13:20
 */
@Component
public class VccFilterInvocationSecurity implements FilterInvocationSecurityMetadataSource {

    private final AntPathMatcher matcher = new AntPathMatcher();

    @Override
    public Collection<ConfigAttribute> getAttributes(Object object) throws IllegalArgumentException {
//        String requestUrl = ((FilterInvocation) object).getFullRequestUrl();
        ManagerVO backend = (ManagerVO) ApplicationUtils.getWorkingUser();
        return SecurityConfig.createList(backend.getRoleIds().toArray(new String[0]));
    }

    @Override
    public Collection<ConfigAttribute> getAllConfigAttributes() {
        return null;
    }

    @Override
    public boolean supports(Class<?> aClass) {
        return FilterInvocation.class.isAssignableFrom(aClass);
    }
}
